package org.jeecg.modules.controller.notify;

import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.jeecg.common.util.CommonUtils;

/**
 * 方法未统一整合，后续整理
 */
@Slf4j
public class WeixinUtil {
    /**
     * 微信视频问诊推送
     */
    public static void push(String openId, String name, String darpment, String doctor, String time, String roomNum, String lastTitle, String url) {
        log.info("--------------进入服务信息推送---------------------" + openId);
        //1,配置
        WxMpInMemoryConfigStorage wxStorage = new WxMpInMemoryConfigStorage();
        WxMpService wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(wxStorage);
        //推送消息
        WxMpTemplateMessage templateMessage = null;

        if (CommonUtils.IS_ZS) {
            //正式站
            wxStorage.setAppId("wxb1a6ee9de43f2507");//appid
            wxStorage.setSecret("e3f90f4011f9ca6f335461c773e228ec");//appsecret
            templateMessage = WxMpTemplateMessage.builder()
                    .toUser(openId)//要推送的用户openid
                    .templateId("YRYEKLj41n_ztJTTCEdHzX1adVk35EIwTtscKGyDUVg")
                    .url(url)//点击模板消息要访问的网址
                    .build();
        } else {
            //大医记忆
            wxStorage.setAppId("wx5efe1f7c4a041b41");//appid
            wxStorage.setSecret("3954a8a78ceb32137336a7da409048bd");//appsecret
            templateMessage = WxMpTemplateMessage.builder()
                    .toUser(openId)//要推送的用户openid
                    .templateId("uQPzRmnLN-OAHaZbpVzHO-Hr2IPiNPs9K-0X9GCUWyQ")//模板id
                    .url(url)//点击模板消息要访问的网址
                    .build();
        }
        //3,如果是正式版发送消息，，这里需要配置你的信息
//        templateMessage.addData(new WxMpTemplateData("first", "功能测试请忽略：", "#FF00FF"));
//        templateMessage.addData(new WxMpTemplateData("keyword1", "张三", "#FF00FF"));
//        templateMessage.addData(new WxMpTemplateData("keyword2", "内科", "#FF00FF"));
//        templateMessage.addData(new WxMpTemplateData("keyword3", "张医生", "#FF00FF"));
//        templateMessage.addData(new WxMpTemplateData("keyword4", "2021年8月19日", "#FF00FF"));
//        templateMessage.addData(new WxMpTemplateData("remark", "大医智慧", "#FF00FF"));


        templateMessage.addData(new WxMpTemplateData("first", "您好，您已成功预约视频问诊", "#FF00FF"));
        templateMessage.addData(new WxMpTemplateData("keyword1", name, "#000000"));
        templateMessage.addData(new WxMpTemplateData("keyword2", darpment, "#000000"));
        templateMessage.addData(new WxMpTemplateData("keyword3", doctor, "#000000"));
        templateMessage.addData(new WxMpTemplateData("keyword4", time, "#000000"));
        templateMessage.addData(new WxMpTemplateData("keyword5", roomNum, "#000000"));
        templateMessage.addData(new WxMpTemplateData("remark", lastTitle, "#000000"));

        try {
            String msg = wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
            log.info("推送成功:" + msg);
        } catch (Exception e) {
            log.info("推送成功:" + e.getMessage());
            e.printStackTrace();
        }


    }

    /**
     * 微信挂号成功推送
     */
    public static void pushRegisterSuccess(String openId, String name, String darpment, String darpmentDoctor, String time, String address, String lastTitle, String url) {
        log.info("--------------进入微信挂号成功推送---------------------" + openId);
        //1,配置
        WxMpInMemoryConfigStorage wxStorage = new WxMpInMemoryConfigStorage();
        WxMpService wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(wxStorage);
        //推送消息
        WxMpTemplateMessage templateMessage = null;

        if (CommonUtils.IS_ZS) {
            //正式站
            wxStorage.setAppId("wxb1a6ee9de43f2507");//appid
            wxStorage.setSecret("e3f90f4011f9ca6f335461c773e228ec");//appsecret
            templateMessage = WxMpTemplateMessage.builder()
                    .toUser(openId)//要推送的用户openid
                    .templateId("Rx9dsJ0DfysuS-NjZ2Le8PxYkkcmjzzNe2JwGYPoegg")//模板id
                    .url(url)//点击模板消息要访问的网址
                    .build();
        } else {
            //大医记忆
            wxStorage.setAppId("wx5efe1f7c4a041b41");//appid
            wxStorage.setSecret("3954a8a78ceb32137336a7da409048bd");//appsecret
            templateMessage = WxMpTemplateMessage.builder()
                    .toUser(openId)//要推送的用户openid
                    .templateId("sEFTMHgBfVS4wW8rSS81mxIjBCRN_arISX_dEXkG1vE") //大医记忆
                    .url(url)//点击模板消息要访问的网址
                    .build();
        }

        //3,如果是正式版发送消息，，这里需要配置你的信息
        templateMessage.addData(new WxMpTemplateData("first", "您好，您已成功预约", "#000000"));
        templateMessage.addData(new WxMpTemplateData("keyword1", name, "#000000"));
        templateMessage.addData(new WxMpTemplateData("keyword2", darpment, "#000000"));
        templateMessage.addData(new WxMpTemplateData("keyword3", darpmentDoctor, "#000000"));
        templateMessage.addData(new WxMpTemplateData("keyword4", time, "#000000"));
        templateMessage.addData(new WxMpTemplateData("keyword5", address, "#000000"));
        templateMessage.addData(new WxMpTemplateData("remark", lastTitle, "#000000"));

        try {
            String msg = wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
            log.info("推送成功:" + msg);
        } catch (Exception e) {
            log.info("推送成功:" + e.getMessage());
            e.printStackTrace();
        }


    }

    /**
     * 微信挂号失败推送
     */
    public static void pushRegisterFalse(String openId, String name, String darpment, String darpmentDoctor, String time, String address, String lastTitle, String url) {
        log.info("--------------进入微信挂号失败推送---------------------" + openId);
        //1,配置
        WxMpInMemoryConfigStorage wxStorage = new WxMpInMemoryConfigStorage();
        WxMpService wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(wxStorage);
        //推送消息
        WxMpTemplateMessage templateMessage = null;


        if (CommonUtils.IS_ZS){
            //正式站
            wxStorage.setAppId("wxb1a6ee9de43f2507");//appid
            wxStorage.setSecret("e3f90f4011f9ca6f335461c773e228ec");//appsecret
            templateMessage = WxMpTemplateMessage.builder()
                    .toUser(openId)//要推送的用户openid
                    .templateId("Rx9dsJ0DfysuS-NjZ2Le8PxYkkcmjzzNe2JwGYPoegg")//模板id
                    .url(url)//点击模板消息要访问的网址
                    .build();
        }else {
            //大医记忆
        wxStorage.setAppId("wx5efe1f7c4a041b41");//appid
        wxStorage.setSecret("3954a8a78ceb32137336a7da409048bd");//appsecret
            templateMessage = WxMpTemplateMessage.builder()
                    .toUser(openId)//要推送的用户openid
                    .templateId("Rx9dsJ0DfysuS-NjZ2Le8PxYkkcmjzzNe2JwGYPoegg")//模板id
                    .url(url)//点击模板消息要访问的网址
                    .build();
        }
            //3,如果是正式版发送消息，，这里需要配置你的信息
        templateMessage.addData(new WxMpTemplateData("first", "您好！您的预约挂号失败，请选择其他医生或挂号时段！", "#000000"));
        templateMessage.addData(new WxMpTemplateData("keyword1", name, "#000000"));
        templateMessage.addData(new WxMpTemplateData("keyword2", darpment, "#000000"));
        templateMessage.addData(new WxMpTemplateData("keyword3", darpmentDoctor, "#000000"));
        templateMessage.addData(new WxMpTemplateData("keyword4", time, "#000000"));
        templateMessage.addData(new WxMpTemplateData("keyword5", address, "#000000"));
        templateMessage.addData(new WxMpTemplateData("remark", lastTitle, "#000000"));

        try {
            String msg = wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
            log.info("推送成功:" + msg);
        } catch (Exception e) {
            log.info("推送成功:" + e.getMessage());
            e.printStackTrace();
        }


    }
}
